package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.init.SystemInit;
import com.suray.wcs.service.pojo.Rgv;
import com.suray.wcs.service.system.bo.DistRGVStatusBO;
import com.suray.wcs.service.system.entity.RgvDB;
import com.suray.wcs.service.system.mapper.RgvDBMapper;
import com.suray.wcs.service.system.service.IRgvDBService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cooper
 * @since 2019-06-26
 */
@Service
public class RgvDBServiceImpl extends ServiceImpl<RgvDBMapper, RgvDB> implements IRgvDBService {

    /**
     * 小车编号获取DB对象
     *
     * @param rgvNo
     * @return
     */
    @Override
    public RgvDB getRgvDBByNo(Integer rgvNo) {
        QueryWrapper<RgvDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RgvDB::getRgvNo, rgvNo);
        return getOne(queryWrapper);
    }

    /**
     * 更新小车的位置
     * @param rgvdb 需要更新的数据
     * @return
     */
    @Override
    public boolean updateRgvLocation(RgvDB rgvdb) {
        UpdateWrapper<RgvDB> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(RgvDB::getLocation, rgvdb.getLocation())
                .set(RgvDB::getSonRgvLocation, rgvdb.getSonRgvLocation())
                .eq(RgvDB::getRgvId, rgvdb.getRgvId());
        return update(updateWrapper);
    }
    @Override
    public List<DistRGVStatusBO> getDistRGVStatus(String dist) {
        List<DistRGVStatusBO> distRGVStatusBOS = new ArrayList<>();
        for (Rgv rgv : SystemInit.getRgvList()) {
            DistRGVStatusBO distRGVStatusBO = new DistRGVStatusBO();
            distRGVStatusBO.setRgvId(rgv.getRgvDB().getRgvId());
            distRGVStatusBO.setRgvNo(rgv.getRgvDB().getRgvNo());
            distRGVStatusBO.setRgvName(rgv.getRgvName());
            distRGVStatusBO.setCurLocation(rgv.getCurLocation());
            distRGVStatusBO.setCurTask(rgv.getCurTask());
            distRGVStatusBO.setExecutePath(rgv.getWaitReleaseNode());
            distRGVStatusBO.setWaitExecutePath(rgv.getWaitExecuteNodeList());
            distRGVStatusBO.setExecutePaths(rgv.getExecuteNodeList());
            distRGVStatusBO.setRejectStatus(rgv.getRejectStatus());
            distRGVStatusBO.setRejectStatusInfo(rgv.getRejectStatusInfo());
            getRejectDistRGVStatusBO(rgv.getRejectRgv(), distRGVStatusBO, distRGVStatusBO);
            distRGVStatusBOS.add(distRGVStatusBO);
        }
        return distRGVStatusBOS;
    }

    /**
     * 校验小车编号是否重复
     * @param rgvDB
     * @return
     */
    @Override
//    public boolean checkRgvNoUnique(RgvDB rgvDB) {
//        Long rgvId = StringUtils.isNull(rgvDB.getRgvId()) ? -1L : rgvDB.getRgvId();
//        RgvDB info = this.getRgvDBByNo(rgvDB.getRgvNo());
//        if (StringUtils.isNotNull(info) && info.getRgvId().longValue() != rgvId.longValue()) {
//            return false;
//        }
//        return true;
//    }

    public boolean checkRgvNoUnique(RgvDB rgvDB) {
        Long rgvId = StringUtils.isNull(rgvDB.getRgvId()) ? -1L : rgvDB.getRgvId();
        RgvDB info = this.getRgvDBByNo(rgvDB.getRgvNo());
        if(rgvId.longValue()==-1&&StringUtils.isNotNull(info)){
            return true;
        }else if (StringUtils.isNotNull(info) && info.getRgvId().longValue() != rgvId.longValue()) {
            return true;
        }
        return false;
    }
    private void getRejectDistRGVStatusBO(Rgv rgv, DistRGVStatusBO currentDistRGVStatusBO, DistRGVStatusBO rootDistRGVStatusBO) {
        if (rootDistRGVStatusBO.getRejectDistRGVStatusBO() == null
                || currentDistRGVStatusBO.getRgvId().intValue() != rootDistRGVStatusBO.getRgvId().intValue()) {
            if (rgv != null) {
                DistRGVStatusBO distRGVStatusBO = new DistRGVStatusBO();
                distRGVStatusBO.setRgvId(rgv.getRgvDB().getRgvId());
                distRGVStatusBO.setRgvNo(rgv.getRgvDB().getRgvNo());
                distRGVStatusBO.setRgvName(rgv.getRgvName());
                distRGVStatusBO.setCurLocation(rgv.getCurLocation());
                distRGVStatusBO.setCurTask(rgv.getCurTask());
                distRGVStatusBO.setExecutePath(rgv.getWaitReleaseNode());
                distRGVStatusBO.setWaitExecutePath(rgv.getWaitExecuteNodeList());
                distRGVStatusBO.setExecutePaths(rgv.getExecuteNodeList());
                distRGVStatusBO.setRejectStatus(rgv.getRejectStatus());
                currentDistRGVStatusBO.setRejectDistRGVStatusBO(distRGVStatusBO);
            }

        }
    }

    /**
     * 根据车辆编号  修改离线时间
     * @param offLineTime
     * @param rgvNo
     * @return
     */
    @Override
    public boolean updateRgvRemarkOFFLineTime(String offLineTime,Integer rgvNo) {
        UpdateWrapper<RgvDB> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(RgvDB::getRemark,offLineTime).eq(RgvDB::getRgvNo,rgvNo);
        return update(updateWrapper);
    }


}
